using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace SBPweb.Presentation.CompleteMesh.Primitives
{
    public abstract class PrimitiveBase : ModelVisual3D
    {

        #region Protected members

        internal readonly GeometryModel3D rContent = new GeometryModel3D();

        #endregion

        #region Constructor logic

        public PrimitiveBase()
        {
            Content = rContent;
            rContent.Geometry = Tessellate();
        }

        #endregion

        #region Static dependency properties

        public static DependencyProperty MaterialProperty =
            DependencyProperty.Register(
                "Material",
                typeof(Material),
                typeof(PrimitiveBase), new PropertyMetadata(
                    null, new PropertyChangedCallback(OnMaterialChanged)));

        #endregion

        #region Public property accessors

        public Material Material
        {
            get
            {
                return (Material)GetValue(MaterialProperty);
            }
            set
            {
                SetValue(MaterialProperty, value);
            }
        }

        #endregion

        #region Event handlers

        internal static void OnMaterialChanged(Object sender, DependencyPropertyChangedEventArgs e)
        {
            PrimitiveBase p = ((PrimitiveBase)sender);

            p.rContent.Material = p.Material;
        }

        internal static void OnGeometryChanged(DependencyObject d)
        {
            PrimitiveBase p = ((PrimitiveBase)d);

            p.rContent.Geometry = p.Tessellate();
        }

        #endregion

        #region Abstract methods

        internal abstract Geometry3D Tessellate();

        #endregion

    }
}
